﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using YW.IDAL.YWXT;
using YW.DBUtility;//Please add references
namespace YW.SQLServerDAL.YWXT
{
	/// <summary>
	/// 数据访问类:YW_xfjlzb
	/// </summary>
	public partial class YW_xfjlzb:IYW_xfjlzb
	{
		public YW_xfjlzb()
		{}
		#region  BasicMethod

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(Guid ywid)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from YW_xfjlzb");
			strSql.Append(" where ywid=@ywid ");
			SqlParameter[] parameters = {
					new SqlParameter("@ywid", SqlDbType.UniqueIdentifier,16)			};
			parameters[0].Value = ywid;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(YW.Model.YWXT.YW_xfjlzb model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into YW_xfjlzb(");
			strSql.Append("ywid,zbid,xfdh,xflx,EMPI,ksid,ysid,yzid,xfsj,kh,addtime)");
			strSql.Append(" values (");
			strSql.Append("@ywid,@zbid,@xfdh,@xflx,@EMPI,@ksid,@ysid,@yzid,@xfsj,@kh,@addtime)");
			SqlParameter[] parameters = {
					new SqlParameter("@ywid", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@zbid", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@xfdh", SqlDbType.NVarChar,50),
					new SqlParameter("@xflx", SqlDbType.NVarChar,50),
					new SqlParameter("@EMPI", SqlDbType.NVarChar,50),
					new SqlParameter("@ksid", SqlDbType.NVarChar,50),
					new SqlParameter("@ysid", SqlDbType.NVarChar,50),
					new SqlParameter("@yzid", SqlDbType.NVarChar,50),
					new SqlParameter("@xfsj", SqlDbType.DateTime),
					new SqlParameter("@kh", SqlDbType.NVarChar,50),
					new SqlParameter("@addtime", SqlDbType.DateTime)};
			parameters[0].Value = Guid.NewGuid();
			parameters[1].Value = Guid.NewGuid();
			parameters[2].Value = model.xfdh;
			parameters[3].Value = model.xflx;
			parameters[4].Value = model.EMPI;
			parameters[5].Value = model.ksid;
			parameters[6].Value = model.ysid;
			parameters[7].Value = model.yzid;
			parameters[8].Value = model.xfsj;
			parameters[9].Value = model.kh;
			parameters[10].Value = model.addtime;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(YW.Model.YWXT.YW_xfjlzb model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update YW_xfjlzb set ");
			strSql.Append("zbid=@zbid,");
			strSql.Append("xfdh=@xfdh,");
			strSql.Append("xflx=@xflx,");
			strSql.Append("EMPI=@EMPI,");
			strSql.Append("ksid=@ksid,");
			strSql.Append("ysid=@ysid,");
			strSql.Append("yzid=@yzid,");
			strSql.Append("xfsj=@xfsj,");
			strSql.Append("kh=@kh,");
			strSql.Append("addtime=@addtime");
			strSql.Append(" where ywid=@ywid ");
			SqlParameter[] parameters = {
					new SqlParameter("@zbid", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@xfdh", SqlDbType.NVarChar,50),
					new SqlParameter("@xflx", SqlDbType.NVarChar,50),
					new SqlParameter("@EMPI", SqlDbType.NVarChar,50),
					new SqlParameter("@ksid", SqlDbType.NVarChar,50),
					new SqlParameter("@ysid", SqlDbType.NVarChar,50),
					new SqlParameter("@yzid", SqlDbType.NVarChar,50),
					new SqlParameter("@xfsj", SqlDbType.DateTime),
					new SqlParameter("@kh", SqlDbType.NVarChar,50),
					new SqlParameter("@addtime", SqlDbType.DateTime),
					new SqlParameter("@ywid", SqlDbType.UniqueIdentifier,16)};
			parameters[0].Value = model.zbid;
			parameters[1].Value = model.xfdh;
			parameters[2].Value = model.xflx;
			parameters[3].Value = model.EMPI;
			parameters[4].Value = model.ksid;
			parameters[5].Value = model.ysid;
			parameters[6].Value = model.yzid;
			parameters[7].Value = model.xfsj;
			parameters[8].Value = model.kh;
			parameters[9].Value = model.addtime;
			parameters[10].Value = model.ywid;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(Guid ywid)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from YW_xfjlzb ");
			strSql.Append(" where ywid=@ywid ");
			SqlParameter[] parameters = {
					new SqlParameter("@ywid", SqlDbType.UniqueIdentifier,16)			};
			parameters[0].Value = ywid;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string ywidlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from YW_xfjlzb ");
			strSql.Append(" where ywid in ("+ywidlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public YW.Model.YWXT.YW_xfjlzb GetModel(Guid ywid)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 ywid,zbid,xfdh,xflx,EMPI,ksid,ysid,yzid,xfsj,kh,addtime from YW_xfjlzb ");
			strSql.Append(" where ywid=@ywid ");
			SqlParameter[] parameters = {
					new SqlParameter("@ywid", SqlDbType.UniqueIdentifier,16)			};
			parameters[0].Value = ywid;

			YW.Model.YWXT.YW_xfjlzb model=new YW.Model.YWXT.YW_xfjlzb();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public YW.Model.YWXT.YW_xfjlzb DataRowToModel(DataRow row)
		{
			YW.Model.YWXT.YW_xfjlzb model=new YW.Model.YWXT.YW_xfjlzb();
			if (row != null)
			{
				if(row["ywid"]!=null && row["ywid"].ToString()!="")
				{
					model.ywid= new Guid(row["ywid"].ToString());
				}
				if(row["zbid"]!=null && row["zbid"].ToString()!="")
				{
					model.zbid= new Guid(row["zbid"].ToString());
				}
				if(row["xfdh"]!=null)
				{
					model.xfdh=row["xfdh"].ToString();
				}
				if(row["xflx"]!=null)
				{
					model.xflx=row["xflx"].ToString();
				}
				if(row["EMPI"]!=null)
				{
					model.EMPI=row["EMPI"].ToString();
				}
				if(row["ksid"]!=null)
				{
					model.ksid=row["ksid"].ToString();
				}
				if(row["ysid"]!=null)
				{
					model.ysid=row["ysid"].ToString();
				}
				if(row["yzid"]!=null)
				{
					model.yzid=row["yzid"].ToString();
				}
				if(row["xfsj"]!=null && row["xfsj"].ToString()!="")
				{
					model.xfsj=DateTime.Parse(row["xfsj"].ToString());
				}
				if(row["kh"]!=null)
				{
					model.kh=row["kh"].ToString();
				}
				if(row["addtime"]!=null && row["addtime"].ToString()!="")
				{
					model.addtime=DateTime.Parse(row["addtime"].ToString());
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ywid,zbid,xfdh,xflx,EMPI,ksid,ysid,yzid,xfsj,kh,addtime ");
			strSql.Append(" FROM YW_xfjlzb ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" ywid,zbid,xfdh,xflx,EMPI,ksid,ysid,yzid,xfsj,kh,addtime ");
			strSql.Append(" FROM YW_xfjlzb ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM YW_xfjlzb ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.ywid desc");
			}
			strSql.Append(")AS Row, T.*  from YW_xfjlzb T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "YW_xfjlzb";
			parameters[1].Value = "ywid";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

